<template>
	<view class="chapter">
		<view class="chapterText" v-for="(content,index) in charpetContent" :key="index">{{content}}</view>
		<!-- 显示目录 -->
		<view class="bottomLine">
			<text>上一章</text>
			<text @click="showCategory('left')">目录</text>
			<text>下一章</text>
		</view>
		<view>
			<!-- 普通弹窗 -->
			<uni-popup ref="popup" background-color="#fff" @change="change">
				<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"
					v-for="(list,index) in categoryList" :key="list.chapterId"
					@click="leftCharpetClick(list.chapterId,list.title)"><text
						class="text">{{index+1}}.</text>{{list.title}}</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import api from '@/request/api.js';
	export default {
		data() {
			return {
				chapterId: "",
				title: "",
				charpetContent: [],
				type: 'left',
				showPopup: true,
				bookUuid: "",
				categoryList: []
			}
		},
		onLoad: function(option) {
			console.log('option', option)
			this.chapterId = option.id
			this.title = option.title
			this.bookUuid = option.bookUuid
			// uni.setNavigationBarTitle({
			// 	title: this.title || ''
			// })
			this.getBookChapterList()
		},
		methods: {
			getBookChapterList() {
				api.get('/chapter/v1/get', {
					id: this.chapterId
				}).then(res => {
					this.charpetContent = res.data.content.split(
						'==============================================================================')
					uni.setNavigationBarTitle({
						title: this.title || ''
					})
				}).catch(err => {
					console.log(err)
				})
			},
			showCategory(type) {
				this.type = type
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open(type)
				//重新请求目录接口
				api.get('/tableOf/v1/getByBookUuid', {
					bookUuid: this.bookUuid
				}).then(res => {
					this.categoryList = res.list
				}).catch(err => {
					console.log(err)
				})
			},
			leftCharpetClick(id, title) {
				this.chapterId = id
				this.title = title
				this.getBookChapterList()
			}
		}
	}
</script>

<style scoped>
	.chapter {
		padding: 10px;
		background: #FFFAE8;
	}

	.chapter view {
		font-size: 14px;
		margin-bottom: 15px;
		text-indent: 8%;
	}

	.chapterText {
		margin-bottom: 30px;
	}

	.bottomLine {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 30px;
		line-height: 30px;
		background: rgba(255, 255, 255, 1);
		position: fixed;
		bottom: -16px;
		left: 0;
		right: 0;
	}

	.bottomLine text {
		width: 33.33%;
		text-align: center;
	}

	.popup-height {
		height: 35px;
		line-height: 35px;
		width: 200px;
		border-bottom: 1rpx solid #e3e3e3;
	}

	.text {
		font-size: 12px;
		color: #333;
	}
	/deep/.uni-popup .uni-popup__wrapper.left, .uni-popup .uni-popup__wrapper.right{
		height:360px;
		overflow-y: scroll;
	}
</style>